/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package titanisu.basicMathModule1.integerAdditioSub;

import com.sun.script.javascript.RhinoScriptEngine;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptException;

/**
 *
 * @author Balaji
 */
public class QuestionGeneratorAddSub {

    ArrayList quest = new ArrayList();

    private void setQ(ArrayList arlst) {
        this.quest = arlst;
    }

    public ArrayList getQuest() {
        return quest;
    }

    public void setQuest(ArrayList quest) {
        this.quest = quest;
    }

    /*
     * Generates a single question in below format is forms and array with question and nominator and denominator
     * The final the arraylist is put in variable quest and this methog returns a string putting forward only question
     * quest.get(0) question string
     * quest.get(1) answer nominator
     * quest.get(0) answer denominator
     * 
     * @param: void
     * @returns : String having question
     */
    int toughness = 0;
    public void setToughNess(int toughness)
    {
        this.toughness = toughness;
    }
    
    public String getQuestions() {
        try {

            int x =0, y=0;
if(toughness==1){
            y = new Random().nextInt(29);
            x = new Random().nextInt(29);
    
}
if(toughness==0){
            y = new Random().nextInt(10);
            x = new Random().nextInt(10);
    
}
if(toughness==2){
            y = new Random().nextInt(1000);
            x = new Random().nextInt(1000);
    
}
            
            String op = new titanisu.templates.helperClasses.VariablesHelper().getRandomOperator("+-");
            if (Math.random() > .5)    
            x *= -1;
            
            if (Math.random() > .5)    
            y *= -1;
            
            String xString = "",yString="";
            
            if(y<0)
                yString="(" + y + ")" ;
            else
                yString=""+y;
            
            
            if(x<0)
                xString="(" + x + ")" ;
            else
                xString=""+x;
            
            
                if (Math.random() > .5) {
                    quest.add(0, xString+ op + yString);
                } else {
                    quest.add(0, yString + op + xString);
                }
            

            com.sun.script.javascript.RhinoScriptEngine se = new RhinoScriptEngine();
            String ev = se.eval((String) quest.get(0) + ";").toString();

            ev = ev.substring(0, ev.length() - 2);
            quest.add(1, ev);

            setQ(quest);
            //        System.out.println(" quest set in questionGenerator is " + quest);

        } catch (ScriptException ex) {
            Logger.getLogger(QuestionGeneratorAddSub.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(" the quest is " + quest);
        return (String) quest.get(0);
    }

    /**
     * Gives the answers
     *
     * @return
     */
    public String getAnswer() {
        String answer = "";
        String den = (String) quest.get(1);
        return answer;
    }
}
